package com.situ.service.impl;

import com.situ.bean.PageResult;
import com.situ.bean.Product;
import com.situ.dao.ProductDao;
import com.situ.dao.ProductMapper;
import com.situ.service.IProductService;
import com.situ.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ProductService implements IProductService {

 @Autowired
    private ProductDao productDao;
    @Override
    public List<Product> getAllProducts() {
      return  productDao.getAllProducts();
    }

    @Override
    public Product getProductById(int id) {
      return   productDao.getProductById(id);
    }


    @Override
    public int getTotalProducts() {
        return  productDao.getTotalProducts();
    }

    @Override
    public PageResult<Product> getProducts(int page, int pageSize) {
        // 计算偏移量
        int offset = (page - 1) * pageSize;

        // 查询当前页的数据
        List<Product> products = productDao.getProductsByPage(pageSize, offset);

        // 获取总数据量
        int totalProducts = productDao.getTotalProducts();

        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalProducts / pageSize);

        // 返回分页结果
        return new PageResult<>(products, page, pageSize, totalProducts, totalPages);
    }

    @Override
    public List<Product> searchProducts(String query) {
      return   productDao.searchProducts(query);
    }

    @Override
    public List<Product> filterProducts(List<Integer> categories, Double minPrice, Double maxPrice) {
          return    productDao.filterProducts(categories,minPrice,maxPrice);
    }

    @Override
    public List<Integer> findCategoryIdsByPids(List<Integer> categoryIds) {
        return  productDao.findCategoryIdsByPids(categoryIds);
    }

    @Override
    public List<Product> getAllofProducts() {
        return  productDao.getAllofProducts();
    }


    @Autowired
    private ProductMapper productMapper;

    // 获取所有商品


    // 添加商品
    public void addProduct(Product product) {
        productMapper.addProduct(product);
    }

    // 更新商品
    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }

    // 删除商品
    public void deleteProduct(int id) {
        productMapper.deleteProduct(id);
    }










}
